/**
 * 
 */
package game.breeze.util;

import org.apache.commons.lang.StringUtils;

/**
 * @author wang
 * 
 */
public class StringTools {
	
	/**
	 * 一级分隔符 :(冒号)
	 */
	public static final String leve1split=":";
	
	/**
	 * 二级分隔符 |(竖线)
	 */
	public static final String leve2split="\\|";
	
	/**
	 * 三级分隔符 ;(分号)
	 */
	public static final String leve3split=";";
	/**
	 * 子项分隔符 ,(逗号)
	 */
	public static final String leve4split=",";
	/**
	 * 子项内容分隔符 -(横线)
	 */
	public static final String leve0split="-";
	
	/**
	 * 计算字符串的长度
	 * 
	 * @param str 字符串
	 * 
	 * @return 字符串的长度
	 */
	public static int len(String str) {
		return str.replaceAll("[\\u4E00-\\u9FA5]", "xx").length();
	}


	/**
	 * 取指定长度字符串的值;
	 * 
	 * @param String  str,int byteSize;
	 * @return String;
	 */

	public String getAllStringSize(String str, int byteSize) {
		int len = 0;
		char c;
		String words = "";

		if (str == null || "null".equals(str)) {
			return "";
		}

		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
					|| (c >= 'A' && c <= 'Z')) {
				// 字母, 数字
				len++;
			} else {
				if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]")) { // 中文
					len += 2;
				} else { // 符号或控制字符
					len++;
				}
			}
			words += String.valueOf(c);
			if (len >= byteSize) {//
				words += "..";
				break;
			}
		}
		return words;
	}
	
	/**
	 * 将字符串转换成整形数组
	 * @param s
	 * @param regex 分割符
	 * @return int[] 如果s非法，将返回null
	 */
	public static int[] string2IntArray(String s, String regex) {
		
		if (s != null) {
			
			String[] temp = s.replaceAll("/n", "").replaceAll("/r", "").split(regex);
			
			int l = temp.length;
			
			int[] ia = new int[l];
			
			try {
				
				for (int i = 0; i < l; i++) {
					
					ia[i] = Integer.parseInt(temp[i]);
					
				}
				
				return ia;
				
			} catch (NumberFormatException e) {
				
			}
			
		}
		
		return null;
		
	}
	
	/**
	 * 字符串转义
	 * @param string
	 * @return
	 */
	public static String htmltoString(String string)
	{
		string = StringUtils.stripToEmpty(string);
		string = StringUtils.replace(string, "&", "&amp;");
		string = StringUtils.replace(string, "\"", "&quot;");
		string = StringUtils.replace(string, " ", "&nbsp;");
		string = StringUtils.replace(string, "'", "&#39;");
		string = StringUtils.replace(string, "<", "&lt;");
		string = StringUtils.replace(string, ">", "&gt;");
		string = StringUtils.replace(string, "\t", "   &nbsp;  &nbsp;");
		string = StringUtils.replace(string, "\r", "");
		string = StringUtils.replace(string, "   ", " &nbsp; ");

		return string;
	}
	/**
	 * 字符串转义
	 * @param string
	 * @return
	 */
	public static String stringToHtml(String string)
	{
		string = StringUtils.stripToEmpty(string);
		string = StringUtils.replace(string, "&amp;", "&");
		string = StringUtils.replace(string, "&quot;", "\"");
		string = StringUtils.replace(string, "&nbsp;", " ");
		string = StringUtils.replace(string, "&#39;", "'");
		string = StringUtils.replace(string, "&lt;", "<");
		string = StringUtils.replace(string, "&gt;", ">");
		string = StringUtils.replace(string, "   &nbsp;  &nbsp;", "\t");
		string = StringUtils.replace(string, "&nbsp; ", "    ");

		return string;
	}

	public static String pointToString(int x,int y)
	{
		StringBuffer key = new StringBuffer();
		key.append(x);
		key.append(",");
		key.append(y);
		
		return key.toString();
	}
	
}
